#!/usr/bin/python
# coding:utf-8
import string
import random
import time
from src.utils.models import db


class Classify(db.Model):
    """
    @tableName:thg_classify
    """
    __tablename__ = 'thg_classify'
    id = db.Column(db.String(30), primary_key=True, default='CL'+''.join(
        random.sample(string.ascii_letters + string.digits, 10))+str(int(time.time()*1000)))
    pid = db.Column(db.String(30))
    title = db.Column(db.String(25), nullable=False)

    createdTime = db.Column(db.BigInteger, nullable=False)
    updatedTime = db.Column(db.BigInteger, nullable=False)

    user_id = db.Column(db.Integer, db.ForeignKey('sys_user.id'))
    user = db.relationship("User",
                           backref=db.backref('classifies', lazy='dynamic'))

    def __init__(self, title):
        self.title = title

    def __repr__(self):
        return '<Classify %r>' % self.title


class Context(db.Model):
    """
    @tableName:thg_context
    """
    __tablename__ = 'thg_context'
    id = db.Column(db.String(30), primary_key=True, default='CT'+''.join(random.sample(
        string.ascii_letters + string.digits, 10))+'.'+str(int(time.time()*1000)))
    pid = db.Column(db.String(30))
    title = db.Column(db.String(25), nullable=False)
    full_title = db.Column(db.String(1000), nullable=False)
    state = db.Column(db.String(10))
    #
    createdTime = db.Column(db.BigInteger, nullable=False)
    updatedTime = db.Column(db.BigInteger, nullable=False)

    user_id = db.Column(db.Integer, db.ForeignKey('sys_user.id'))
    user = db.relationship("User",
                           backref=db.backref('contexts', lazy='dynamic'))

    def __init__(self, title, state=None):
        self.title = title
        self.state = state

    def __repr__(self):
        return '<Context %r>' % self.title


# things and tags relationship
ThingsTags = db.Table('thg_things_tags',
                      db.Column('tag_id', db.String(30),
                                db.ForeignKey('thg_tag.id')),
                      db.Column('thing_id', db.String(30),
                                db.ForeignKey('thg_things.id'))
                      )


class Tag(db.Model):
    """
    @tableName:thg_tag
    """
    __tablename__ = 'thg_tag'
    id = db.Column(db.String(30), primary_key=True, default='CG.'+''.join(random.sample(
        string.ascii_letters + string.digits, 10))+'.'+str(int(time.time()*1000)))
    pid = db.Column(db.String(30))
    title = db.Column(db.String(25), nullable=False)
    full_title = db.Column(db.String(1000), nullable=False)

    #
    state = db.Column(db.Integer, nullable=False)
    createdTime = db.Column(db.BigInteger, nullable=False)
    updatedTime = db.Column(db.BigInteger, nullable=False)

    user_id = db.Column(db.Integer, db.ForeignKey('sys_user.id'))
    user = db.relationship("User",
                           backref=db.backref('tags', lazy='dynamic'))

    def __init__(self, title, state=None):
        self.title = title
        self.state = state

    def __repr__(self):
        return '<Context %r>' % self.title


class Things(db.Model):
    """
    @tableName:thg_things
    """
    __tablename__ = 'thg_things'
    id = db.Column(db.String(30), primary_key=True, default='TH'+''.join(random.sample(
        string.ascii_letters + string.digits, 10))+'.'+str(int(time.time()*1000)))
    pid = db.Column(db.String(30), nullable=True)
    title = db.Column(db.String(25), nullable=False)
    fullTitle = db.Column(db.String(500), nullable=False)
    note = db.Column(db.String(500), nullable=False)
    state = db.Column(db.String(10))
    flaged = db.Column(db.String(10))
    note = db.Column(db.String(500))
    # time
    createdTime = db.Column(db.BigInteger, nullable=False, default=time.time())
    updatedTime = db.Column(db.BigInteger, nullable=False, default=time.time())
    completedTime = db.Column(db.BigInteger, nullable=True)
    estimatedTime = db.Column(db.BigInteger, nullable=True)
    deferUntilTime = db.Column(db.BigInteger, nullable=True)
    dueTime = db.Column(db.BigInteger, nullable=True)
    # repeat
    repeat_type = db.Column(db.String(10))
    repeat_next_defered_time = db.Column(db.BigInteger, nullable=False)
    repeat_next_due_time = db.Column(db.BigInteger, nullable=False)
    # classify
    classify_id = db.Column(db.String(30), db.ForeignKey('thg_classify.id'))
    classify = db.relationship("Classify",
                               backref=db.backref('things', lazy='dynamic'))
    # context
    context_id = db.Column(db.String(30), db.ForeignKey('thg_context.id'), nullable=True)
    context = db.relationship("thg_context",
                              backref=db.backref('thg_things', lazy='dynamic'))
    # # tag
    tags = db.relationship('thg_tag', secondary='thg_things_tags',
                           backref=db.backref('thg_things', lazy='dynamic'))
    # user
    user_id = db.Column(db.Integer, db.ForeignKey('sys_user.id'))
    users = db.relationship("User",
                            backref=db.backref('things', lazy='dynamic'))

    def __init__(self, title, state=None):
        self.title = title
        self.state = state

    def __repr__(self):
        return '<Thing %r>' % self.title
